package cn.edu.xmist.supermarket.zpc.controller;

import cn.edu.xmist.supermarket.zpc.Vars;
import cn.edu.xmist.supermarket.zpc.entity.StorePO;
import cn.edu.xmist.supermarket.zpc.entity.ProductPO;
import cn.edu.xmist.supermarket.zpc.service.StoreService;
import cn.edu.xmist.supermarket.zpc.service.ProductService;
import cn.edu.xmist.supermarket.zpc.util.ObjtoLayJson;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商品库
 *
 * @author zpc
 */
@RestController
@RequestMapping("/store")
public class StoreController {

    public static final String[] COLUMNS = {"id", "houseCode", "houseName"};

    @Autowired
    private StoreService storeService;
    @Autowired
    private ProductService wareService;

    @RequestMapping("/list")
    public String list(Integer page, Integer limit) throws Exception {
        List<StorePO> list = storeService.list();

        return ObjtoLayJson.ListtoJson(list, COLUMNS);
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(StorePO house) {
        if (house.getId() == null) {
            QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("house_code", house.getHouseCode());
            List<StorePO> list = storeService.list(queryWrapper);
            if (list.size() > 0) {
                return Vars.FAILURE;
            }
        }
        Boolean flag = storeService.saveOrUpdate(house);
        return Vars.SUCCESS;
    }

    @RequestMapping("/deleteById")
    public String deleteById(Long id) {
        StorePO house = storeService.getById(id);
        QueryWrapper<ProductPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("house_code", house.getHouseCode());
        List<ProductPO> list = wareService.list(queryWrapper);
        if (list.size() > 0) {
            return Vars.FAILURE;
        }
        Boolean flag = storeService.removeById(id);
        return Vars.SUCCESS;
    }
}
